package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.reggie.mapper.DishMapper;
import com.heima.reggie.domain.Dish;
import com.heima.reggie.domain.DishFlavor;
import com.heima.reggie.dto.DishDto;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.DishFlavorService;
import com.heima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author liulanhui
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2023-01-03 14:00:36
*/
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;
    @Resource
    RedisTemplate redisTemplate;

    @Override
    public List<Dish> selectDishByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.selectAllByCategoryId(categoryId);
        return dishes;
    }

    @Override
    @Transactional
    public void saveDishAndFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(int i = 0; i < flavors.size();++i){
            DishFlavor dishFlavor = flavors.get(i);
            dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);


    }

    @Override
    @Transactional
    /**
     * 分页查询
     */
    public List<DishDto> getCategoryName(Integer page , Integer pageSize) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Dish> list = this.list();
        List<DishDto> dishDtos = new ArrayList<>();
        System.out.println(list.size());
        for(int i = 0; i < list.size(); i++){
            Dish dish = list.get(i);
            Long categoryId = dish.getCategoryId();

            String categoryName = categoryService.getCategoryNameById(categoryId);
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setCategoryName(categoryName);
            dishDtos.add(i,dishDto);
        }

        return dishDtos;
    }

    @Override
    public DishDto getDishDtoById(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish,dishDto);
        Long categoryId = dish.getCategoryId();
        String categoryName = categoryService.getCategoryNameById(categoryId);
        dishDto.setCategoryName(categoryName);
        List<DishFlavor> dishFlavorList = dishFlavorService.getDishFlavorByDishId(id);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;

    }

    @Override
    @Transactional
    public void updateDishAndFlavor(DishDto dishDto) {
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(int i = 0; i < flavors.size();++i){
            DishFlavor dishFlavor = flavors.get(i);
            dishFlavor.setDishId(dishId);
        }
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDto,dish,"categoryName","flavors","copies");
        log.info("dish ================================================={}",dish);
        this.updateById(dish);

        dishFlavorService.deleteByDishId(dishId);
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        this.removeByIds(longs);
        dishFlavorService.deleteByDishIdIn(longs);
    }

    @Override
    public void updateStatusByIdIn(Integer status , Long[] ids) {
        List<Long> longs = Arrays.asList(ids);
        dishMapper.updateStatusByIdIn(status,longs);
    }


}




